package org.dizem.freepl0.gui.controller;

import org.dizem.freepl0.compiler.*;
import org.dizem.freepl0.compiler.errormanager.ErrorInfo;
import org.dizem.freepl0.gui.listener.FPListener;
import org.dizem.freepl0.gui.menu.FPToolBar;
import org.dizem.freepl0.gui.panel.EditPanel;
import org.dizem.freepl0.gui.panel.OutputPanel;
import org.dizem.freepl0.gui.panel.TabPanel;


import javax.swing.*;
import javax.swing.filechooser.*;
import java.io.*;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: DIZEM
 * Date: 11-2-2
 * Time: 下午3:43
 */

public class FPController {

	private FPListener listener;
	private TabPanel tabPanel;
	private OutputPanel outputPanel;
	private OutputPanel targetCodePanel;
	private FPToolBar toolBar;

	private FPCompiler compiler;

	public FPController() {
		listener = new FPListener(this);
		tabPanel = new TabPanel(this);
		toolBar = new FPToolBar(this);
		outputPanel = new OutputPanel();
		targetCodePanel = new OutputPanel();

	}

	public void showAbout() {
		JOptionPane.showMessageDialog(tabPanel,
				"FreePL0 v0.5\n\nAuthor: Dizem\nDate:2011/2/2",
				"About",
				JOptionPane.INFORMATION_MESSAGE);
	}

	public void loadFile() {
		for (File file : new File(".").listFiles()) {
			if (file.getName().toLowerCase().endsWith(".pl0")) {
				tabPanel.addNewTab(file);
			}
		}
	}

	public void run() {
		compiler.getTargetCodeManager().run(getOutputPanel());
	}

	public void addNewTab(String filename) {
		tabPanel.addNewTab("", filename);
	}

	class Filter extends javax.swing.filechooser.FileFilter
	{
		public boolean accept(File file) {
			return (file.getName().endsWith(".pl0") || file.isDirectory());
		}

		public String getDescription() {
			return ("PL0 Source Files(*.pl0)");
		}
	}

	public void openFile() {
		JFileChooser chooser = new JFileChooser(".");
		chooser.setFileFilter(new Filter());
		if (chooser.showOpenDialog(tabPanel) == JFileChooser.APPROVE_OPTION) {

			tabPanel.addNewTab(chooser.getSelectedFile());
		}
	}

	public void saveFile() {
		for (int i = 0; i < tabPanel.getTabCount(); ++i) {
			EditPanel editor = (EditPanel) ((JScrollPane) tabPanel.getComponent(i)).getViewport().getView();
			File f = new File(editor.getFilename());
			try {
				if (f.exists())
					f.delete();
				f.createNewFile();
				PrintStream p = new PrintStream(f);
				p.print(editor.getText());
				p.close();
			} catch (IOException e) {
				e.printStackTrace();
			}

		}
	}

	public boolean compile() {
		outputPanel.clearText();
		compiler = new FPCompiler(tabPanel.getCurrentTab().getText());
		compiler.compile();
		List<ErrorInfo> list = compiler.getErrorManager().getErrorInfoList();
		int len = compiler.getTargetCodeManager().getCodeIndex();
		for (int i = 0; i < len; ++i) {
			targetCodePanel.appendText(compiler.getTargetCodeManager().listCode(i) + "\n");
		}

		for (ErrorInfo error : list) {
			outputPanel.appendText(error.toString());
		}

		if (list.size() == 0) {
			outputPanel.appendText("no error.\n");
			return true;

		} else {
			outputPanel.appendText(list.size() + " errors.\n");
			return false;
		}
	}

	public OutputPanel getTargetCodePanel() {
		return targetCodePanel;
	}

	public FPToolBar getToolBar() {
		return toolBar;
	}

	public FPListener getListener() {
		return listener;
	}

	public TabPanel getTabPanel() {
		return tabPanel;
	}

	public OutputPanel getOutputPanel() {
		return outputPanel;
	}

	public void exit() {
		int result = JOptionPane.showConfirmDialog(tabPanel, "Save all files before exit?");
		if (result == JOptionPane.YES_OPTION) {
			saveFile();
			System.exit(0);

		} else if (result == JOptionPane.NO_OPTION) {
			System.exit(0);
		}
	}
}
